<%@ taglib prefix="page" uri="http://com.qipay/pagelib" %>
<%@ page language="java"  contentType="text/html;charset=utf-8" %>
<%@ page import="com.qipay.admin.tags.PageModel" %>
<%@ page import="com.qipay.admin.utils.SiteHelper" %>
<%@ page import="com.qipay.baas.model.CpAccount" %>
<%@ page import="com.qipay.baas.model.User" %>
<%@ page import="com.qipay.user.entity.UserLevel" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.List" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="java.math.BigDecimal" %>
<%@ page import="com.qipay.admin.utils.DateUtils" %>
<!doctype html>
<html>
<head>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <jsp:include page="head.jsp"></jsp:include>
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/list.css">
    <title>商户列表</title>
    <script type="text/javascript">
        //document.getElementsByTagName("html")[0].style.width=screen.availWidth/100+"px";
        document.getElementsByTagName("html")[0].style.fontSize=screen.availWidth/200+"px";
    </script>
</head>
<body class="devpreview skin-blue ">
<div class="wrapper">
    <jsp:include page="nav.jsp"></jsp:include>
    <jsp:include page="left.jsp"></jsp:include>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box border">
                        <div id="searchForm">
                            <div class="new-group">
                            <label for="txtName">商户名称：</label>
                                <input type="text" id="txtName" value="<%= request.getParameter("name")!=null?request.getParameter("name"):""%>"/>
                            </div>
                            <div class="new-group">
                                <label for="txtcpid"> 商户ID：</label>
                                <input type="text" id="txtcpid" value="<%= request.getParameter("cpId")!=null?request.getParameter("cpId"):""%>"/>
                            </div>
                            <div class="new-group">
                                <label for="txtContract"> 联系人：</label>
                                <input type="text" id="txtContract" value="<%= request.getParameter("contact")!=null?request.getParameter("contact"):""%>"/>
                            </div>

                            <div class="new-group">
                                <label for="ddltype">状态：</label>
                                <select id="ddltype">
                                    <option value="-1">全部</option>
                                    <option value="0">禁用</option>
                                    <option value="1">审核中</option>
                                    <option value="2">启用</option>
                                </select>
                            </div>
                            <div class="button-group">
                                <button type="button" class="btn btn-primary btn-sm borderNone" id="btnSearch" >查询</button>
                            </div>
                            <div style="clear:both;"></div>
                        </div>
                        <%
                            CpAccount tongji = (CpAccount) request.getAttribute("totalList");
                        %>
                        <!-- /.box-header -->
                        <div class="tableContent">
                            <table id="pLst"  class="table table-bped table-hover">
                                <thead>
                                <tr>
                                    <th width="8%">商户ID</th>
                                    <th width="12%">商户名称</th>
                                    <th width="8%">代理ID</th>
                                    <th width="8%">余额</th>
                                    <th width="8%">可提</th>
                                    <th width="8%">不可提</th>
                                    <th width="10%">已提金额</th>
                                    <th width="8%">手续费</th>
                                    <th width="20%">操作</th>
                                </tr>
                                <tr>
                                    <th width="8%"></th>
                                    <th width="12%"></th>
                                    <th width="8%"></th>
                                    <th width="8%"><span style="color: green;"><%=new BigDecimal(tongji.getBalance()).divide(new BigDecimal(String.valueOf("100")))%></span></th>
                                    <th width="8%"><span style="color: green;"><%= new BigDecimal(tongji.getBalanceAvailable()).divide(new BigDecimal(String.valueOf("100")))%></span></th>
                                    <th width="8%"><span style="color: green;"><%= new BigDecimal(tongji.getBalanceDisabled()).divide(new BigDecimal(String.valueOf("100")))%></span></th>
                                    <th width="10%"><span style="color: green;"><%= new BigDecimal(tongji.getWidthdrawAmount()).divide(new BigDecimal(String.valueOf("100")))%></span></th>
                                    <th width="8%"><span style="color: green;"><%= new BigDecimal(tongji.getFee()).divide(new BigDecimal(String.valueOf("100")))%></span></th>
                                    <th width="20%"></th>
                                </tr>
                                </thead>
                                <tbody>
                                <% List<CpAccount> articles = (List<CpAccount>) request.getAttribute("lst");
                                    PageModel pager = (PageModel) request.getAttribute("page");
                                    User currentUser = (User) session.getAttribute("user");
                                    UserLevel userLevel = SiteHelper.getUserLevelByLevel(currentUser.getLevel());
                                    String query = pageContext.getRequest().getServletContext().getContextPath() + "/cp" + pager.getUrl();
                                    //SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                    if (articles != null && articles.size() > 0) {
                                        Iterator<CpAccount> iter = articles.iterator();
                                        CpAccount user = null;
                                        while (iter.hasNext()) {
                                            user = iter.next();
                                %>
                                <tr id="user_<%=user.getCpId()%>">
                                    <td>
                                        <a href="${pageContext.request.contextPath}/cp/<%= user.getCpId()%>" style="color: #00a65a">
                                            <%= user.getCpId()%>
                                        </a>
                                    </td>

                                    <td>
                                        <a href="${pageContext.request.contextPath}/cp/<%= user.getCpId()%>" style="color: #00a65a">
                                            <%= user.getName()%>
                                        </a>
                                    </td>
                                    <td>
                                        <%=user.getAgentId() %>
                                    </td>
                                    <td>
                                        <%= SiteHelper.getMoneyFormatAll(user.getBalance()/100.0)%>
                                    </td>
                                    <td>
                                        <%= SiteHelper.getMoneyFormatAll(user.getBalanceAvailable()/100.0)%>
                                    </td>
                                    <td>
                                        <%= SiteHelper.getMoneyFormatAll(user.getBalanceDisabled()/100.0)%>
                                    </td>
                                    <td>
                                        <%= SiteHelper.getMoneyFormatAll(user.getWidthdrawAmount()/100.0)%>
                                    </td>
                                    <td>
                                        <%= SiteHelper.getMoneyFormatAll(user.getFee()/100.0)%>
                                    </td>

                                    <td>
                                        <div class="button-group">
                                        <a href="${pageContext.request.contextPath}/cp/<%= user.getCpId()%>/channel">
                                            <button type="button" class="btn btn-primary btn-sm borderNone" >渠道</button></a>
                                        <% if (userLevel == UserLevel.ADMIN) {%>
                                        <%--<a name="lnkShowForbin" s="300" pk="<%= user.getCpId()%>" href="javascript:;">
                                            <button type="button" class="btn <%= user.getAccountState() != null && user.getAccountState() == 0 ? "btn-success" : "btn-danger"%> btn-sm borderNone" >
                                                <%= user.getAccountState() != null && user.getAccountState() == 0 ? "启用" : "禁用"%></button></a>--%>
                                        <a name="lnkDelete" s="<%= user.getAccountState()%>" pk="<%= user.getCpId()%>" href="javascript:;">
                                                <button type="button" class="btn <%= user.getAccountState() != null && user.getAccountState() == 0 ? "btn-success" : "btn-danger"%> btn-sm borderNone" >
                                                    <%= user.getAccountState() != null && user.getAccountState() == 0 ? "启用" : "禁用"%>
                                                </button>
                                            </a>

                                        <a href="${pageContext.request.contextPath}/cp/<%= user.getCpId()%>">
                                            <button type="button" class="btn btn-success btn-sm borderNone" >修改</button></a>
                                        <a  name="lnkRemove" href="javascript:;" pk="<%= user.getCpId()%>">
                                            <button type="button" class="btn btn-danger btn-sm borderNone" >删除</button>
                                        <a  href="javascript:;" pk="<%= user.getCpId()%>">
                                                <button type="button" class="btn btn-success btn-sm borderNone jiajianModal0"
                                                        data-toggle="modal"
                                                        data-target="#jiajianModal">加减</button>
                                        </a>
                                         <button type="button" class="btn btn-warning btn-sm borderNone jiajianModalBtn"
                                                    pk="<%= user.getCpId()%>" data-toggle="modal"
                                                    data-target="#jiajianListModal" pk="">加减记录</button>
                                        <%}%>
                                    </td>
                                </tr>

                                <tr id="trForbin<%=user.getCpId()%>" style="display: none">
                                    <td colspan="8"></td>
                                    <td colspan="4">
                                    <input type="text" style="width: 333px" id="txtForbin<%=user.getCpId()%>" placeholder="请输入禁用的理由">
                                    <a name="btnDelete" pk="<%= user.getCpId()%>" href="javascript:;">
                                        <button type="button" class="button back-green">确定</button>
                                    </a></td>
                                </tr>
                                <%
                                    }
                                } else {
                                %>
                                <tr>
                                    <td colspan="12">未找到数据</td>
                                </tr>
                                <% }%>
                                </tbody>
                            </table>
                            <div class="col-sm-10">
                                <div class="dataTables_paginate paging_simple_numbers" id="example1_paginate">
                                    <page:page totalSum="<%= pager.getTotal()%>"
                                               url="<%= query%>"
                                               pageNo="<%= pager.getPage()%>"
                                               pageSize="<%= pager.getPageSize()%>"></page:page>
                                </div>
                            </div>
                            <div style="clear: both"></div>

                        </div>
                    </div>
                </div>
            </div>
     </section>
    </div>
<%--<jsp:include page="foot.jsp"></jsp:include>--%>
</div>
<div class="modal fade" id="jiajianListModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title control-label" ><span id="qwere"></span>余额加减记录</h4>
            </div>
            <div class="modal-body">
                <table id="jiajianList" class="table table-bped table-hover">
                    <thead>
                    <tr>
                        <th width="10%">操作金额</th>
                        <th width="20%">操作时间</th>
                        <th width="20%">备注</th>
                        <th width="15%">操作人</th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
                <input name="spChannelId" type="hidden" value="0">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">返回</button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="jiajianModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title control-label" >加减商户<span id="dgfhgh"></span>的余额</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="save" method="POST" id="doc-vld-msg">
                    <div class="box-body" id="dvContent">
                        <div class="form-group form-group-md">
                            <label for="money" class="col-sm-3 control-label">金额：</label>
                            <div class="col-sm-9">
                                <input class="form-control" type="number" id="money" maxlength="20"
                                       name="money"
                                       data-validation-message="请输入金额" required>
                            </div>
                        </div>
                        <div class="form-group form-group-md">
                            <label for="remark" class="col-sm-3 control-label">备注：</label>
                            <div class="col-sm-9">
                                <input class="form-control" type="" id="remark" maxlength="20"
                                       name="remark"
                                       data-validation-message="请输入备注" required>
                            </div>
                        </div>

                        <div class="form-group form-group-md">
                            <label for="code" class="col-sm-3 control-label">验证码：</label>
                            <div class="col-sm-9">
                                <input class="form-control" type="" id="code" maxlength="20"
                                       name="code"
                                       data-validation-message="验证码" required>
                            </div>
                        </div>
                        <input type="text" id="cpId" hidden>
                        <input id="adsfsg" hidden/>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">放弃</button>
                <button type="button" class="btn btn-primary" id="jiajianModalSureBtn">提交</button>
            </div>
        </div>
    </div>
</div>
<script src="<%= request.getAttribute("StaticHost") %>/js/jquery.slimscroll.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/fastclick.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/adminlte.min.js"></script>

<script>
    var dmoTr = "<tr id=\"dmoTr\">\n" +
        "            <td>-100</td>\n" +
        "            <td>2020-05-15 00:00:00</td>\n" +
        "            <td>备注</td>\n" +
        "            <td>user</td>\n" +
        "        </tr>";
    $('.jiajianModalBtn').on("click",function () {
        var cpId=$(this).attr('pk');
        $('#adsfsg').val(cpId);
        $('#qwere').html(cpId);
        initJiajian();
    });

    //数据初始化
    function initJiajian() {
        //获取当前供号商ID，当前代理商ID
        var cpId = $('#adsfsg').val();
        $.ajax({
            type: 'GET',
            url: "${pageContext.request.contextPath}/cp/getJiajian",
            data: { cpId: cpId},
            dataType: "json",
            success: function (data) {
                if (data) {
                    if (data.code == 200) {
                        //获取所有商户
                        var cpWithdraws = data.result;
                        //清除tbody的数据
                        var $tbody = $('#jiajianList').find('tbody');
                        $tbody.html('');
                        if(cpWithdraws.length>0){
                            //展示数据
                            var $dmoTr = $(dmoTr);
                            for(var i=0;i<cpWithdraws.length;i++){
                                var cpWithdraw = cpWithdraws[i];
                                var $tr = $dmoTr.clone();
                                $tr.removeAttr("id");
                                var abc = cpWithdraw.amount/100;
                                if(abc*1>0) {
                                    abc="-"+abc;
                                }else {
                                    abc="+"+abc*-1;
                                }
                                $tr.find("td").eq(0).html(abc);
                                $tr.find("td").eq(1).html(format(cpWithdraw.createTime));
                                $tr.find("td").eq(2).html(cpWithdraw.remark);
                                $tr.find("td").eq(3).html(cpWithdraw.attach);
                                $tbody.append($tr);
                            }
                        }else {
                            $tbody.html('<tr>\n' +
                                '                                    <td colspan="4">未找到数据</td>\n' +
                                '                                </tr>');
                        }

                    }else {
                        alert("获取失败！");
                    }
                }
            },
            error: function (error) {
                console.log(error);
                alert('服务器异常，请稍后再试！');
            }
        })
    }

    $('.jiajianModal0').on("click",function () {
        var cpId = $(this).parent().attr("pk");
        $('#cpId').val(cpId);
        $('#dgfhgh').html(cpId);
    });
    $("#jiajianModalSureBtn").on("click", function () {
        var cpId = $.trim($("#cpId").val()) || "";
        var money = $.trim($("#money").val()) || "";
        var remark = $.trim($("#remark").val()) || "";
        var code = $.trim($("#code").val()) || "";
        if (cpId==""||money =="" || code==""|| remark=="") {
            alert("请输入信息");
            return false;
        }

        var that = this;
        $(that).attr("disabled", "disabled");
        if (confirm("确定给商户"+cpId+"余额充值"+money+"元吗？")) {
            $.ajax({
                type: 'POST',
                url: "${pageContext.request.contextPath}/cp/chongzhi",
                data: { cpId: cpId,money: money,code: code,remark: remark},
                dataType: "json",
                success: function (data) {
                    if (data) {
                        if (data.code == 200) {
                            alert(data.message);
                            window.location.reload();
                            return;
                        }else {
                            alert(data.message);
                            $("#code").val('')
                        }
                    }
                    $(that).removeAttr("disabled");
                },
                error: function (error) {
                    console.log(error);
                    alert('服务器异常，请稍后再试！');
                    $(that).removeAttr("disabled");
                }
            })
        }

    });

    $("[name='lnkShow']").click(function () {
        var cpId = $(this).attr("pk");
        var Contect=$(this).attr("pk1");
        var Moblie=$(this).attr("pk2");
        var accountState=$(this).attr("pk3");
        if(accountState==2){
            accountState="启用";
        }else{
            accountState="禁用";
        }
        var cpCreateTime=$(this).attr("pk4");
        var cpName=$(this).attr("pk5");
        var Agentid=$(this).attr("pk6");
        var ContractState=$(this).attr("pk7");
        if(ContractState==1){
            ContractState="已签约";
        }else{
            ContractState="未签约";
        }
        alert("当前商户ID："+cpId+"\n商户名称："+cpName+"\n代理商ID："+Agentid+"\n联系人："
            +Contect+"\n联系人手机:"+Moblie+"\n状态:"+accountState+"\n签约:"+ContractState+
            "\n创建时间:"+cpCreateTime);
    });

    $(function () {
        var channelState = parseInt("<%= StringUtils.isNotEmpty(request.getParameter("s"))?request.getParameter("s"):-1%>", 10);
        $("#ddltype").val(channelState);
        $("#btnSearch").on("click", function () {
            var name = $.trim($("#txtName").val());
            var cpId = $.trim($("#txtcpid").val());
            var c = $.trim($("#txtContract").val());
            var m = $.trim($("#txtMobile").val());
            var agent = '<%= request.getParameter("agent")!=null&&request.getParameter("agent").endsWith("1")?"&agent=1":""%>'
            var resm = /^0?(13|15|17|18)[0-9]{9}$/;
            var s = $("#ddltype").val();
            if (s == null || s == "null") {
                s = -1;
            }
            if (m != "" && !resm.test(m)) {
                alert("手机号格式不正确。");
                return;
            }
            window.location.href = "${pageContext.request.contextPath}/cp?name=" + encodeURIComponent(name) + "&cpId=" + encodeURIComponent(cpId) + "&contact=" + encodeURIComponent(c) + "&m=" + encodeURIComponent(m) + agent+ "&s=" + (s == -1 ? "" : s);
        });

        $("[name='lnkShowForbin']").click(function () {
            var pk = $(this).attr("pk");
            $("#trForbin" + pk).show(200);
        });
        $("[name='btnDelete']").click(function () {
            var pk = $(this).attr("pk");
            var info = $("#txtForbin" + pk).val();
            if ($.trim(info) == "") {
                alert("请输入禁用的原因");
                return;
            }
            if ($.trim(info).length > 50) {
                alert("禁用原因只能50字以内");
                return;
            }
            info = encodeURIComponent(info);
            if (pk > 0) {
                $.ajax({
                    url: "${pageContext.request.contextPath}/ajax?n=setcpaccountstate&id=" + pk + "&id2=0&s=" + info,
                    type: "GET",
                    async: true,//true异步 false同步
                    context: this,
                    timeout: 10000,
                    success: function (data) {
                        if (data != null && data == "1") {
                            $("#trgoods" + pk).removeClass("success");
                            $("#trgoods" + pk).html("禁用");
                            $("#trForbin" + pk).hide(200);
                        }
                        else {
                            alert("fail:" + data);
                        }
                    },
                    error: function (d, s, b) {
                        alert("失败:" + b);
                    }
                });
            }
        });
        $("[name='lnkDelete']").click(function () {
            var pk = $(this).attr("pk");
            var state = $(this).attr("s");
            var isSuccess = state == "2";
            if (confirm(isSuccess ? "确定禁用吗？" : "确定启用吗？")) {
                state = isSuccess ? 0 : 200;
                if (pk > 0) {
                    $.ajax({
                        url: "${pageContext.request.contextPath}/ajax?n=setcpaccountstate&id=" + pk + "&id2=" + state,
                        type: "GET",
                        async: true,//true异步 false同步
                        context: this,
                        timeout: 10000,
                        success: function (data) {
                            if (data != null && data == "1") {
                                if (isSuccess) {
                                    $("#trgoods" + pk).removeClass("danger").addClass("success");
                                    $(this).find('button').removeClass("btn-danger").addClass("btn-success").html("启用");
                                    $(this).attr("s", "0");
                                }else {
                                    $("#trgoods" + pk).removeClass("success").addClass("danger");
                                    $(this).find('button').removeClass("btn-success").addClass("btn-danger").html("禁用");
                                    $(this).attr("s", "2");
                                }
                            }
                            else {
                                alert("fail:" + data);
                            }
                        },
                        error: function (d, s, b) {
                            alert("失败:" + b);
                        }
                    });
                }
            }
        });
        $("[name='lnkRemove']").click(function () {
            var pk = $(this).attr("pk");
            if (pk > 0) {
                if (confirm("确定删除吗？")) {
                    $.ajax({
                        url: "${pageContext.request.contextPath}/ajax?n=delcpbyid&id=" + pk,
                        type: "GET",
                        async: true,//true异步 false同步
                        context: this,
                        timeout: 10000,
                        success: function (data) {
                            if (data != null && data == "1") {
                                $("#user_" + pk).hide(100);
                            }
                            else {
                                alert("fail:" + data);
                            }
                        },
                        error: function (d, s, b) {
                            alert("失败:" + b);
                        }
                    });
                }
            }

        });
    })

    function format(shijian){
        var date = new Date(shijian)
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h=h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        minute = minute < 10 ? ('0' + minute) : minute;
        var second=date.getSeconds();
        second=second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
    }
</script>
</body>
</html>

